package clock;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.beans.PropertyChangeEvent;
import java.text.DecimalFormat;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingConstants;

/**
 * An interactive interface for the AlarmClock class.
 */

public class AlarmClockPresentation
    extends JPanel
    implements Presentation, EventLabels {

  private JButton         hourButton;
  private JButton         minuteButton;
  private JRadioButton onButton;
  private JRadioButton offButton;
  private JRadioButton setButton;
  private JLabel  valueDisplay1;
  private JLabel  valueDisplay2;
  private JLabel  valueDisplay3;
  private JLabel  modeDisplay;
  private JPanel modePanel = new JPanel();
  private JPanel valuePanel   = new JPanel();
  private JPanel controlPanel = new JPanel();

  
  private AlarmClockTranslation translation;

  private DecimalFormat format = new DecimalFormat("00");
  GridLayout gridLayout1 = new GridLayout();
  GridLayout gridLayout2 = new GridLayout(1,3);
  BorderLayout borderLayout1 = new BorderLayout();
  BorderLayout borderLayout2 = new BorderLayout();

  public AlarmClockPresentation(AlarmClockTranslation translation) {
    // first hook up the this presentation to the translation
    this.translation = translation;

    // then build the interface the JBuilder way
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }

    // finally hook up the translation to this presentation
    translation.setVisibleInterface(this);
  } // end AlarmClockPresentation constructor.

  private void jbInit() throws Exception {

    // make layout and other general decisions here

    setFont(new Font("Lucida Grande", Font.PLAIN, 20));
    this.setLayout(borderLayout1);

    // build the visible interface

    valueDisplay1 = new JLabel();
    valueDisplay1.setFont(new java.awt.Font("Lucida Grande", 0, 72));
    valueDisplay1.setPreferredSize(new Dimension(116, 73));
    valueDisplay1.setVerifyInputWhenFocusTarget(true);
    valueDisplay1.setHorizontalAlignment(SwingConstants.RIGHT);
    valueDisplay1.setText("abc");
    valueDisplay1.setVerticalAlignment(SwingConstants.CENTER);
    
    valueDisplay2 = new JLabel();
    valueDisplay2.setFont(new java.awt.Font("Lucida Grande", 0, 40));
    valueDisplay2.setPreferredSize(new Dimension(20, 20));
    valueDisplay2.setVerifyInputWhenFocusTarget(true);
    valueDisplay2.setHorizontalAlignment(SwingConstants.CENTER);
    valueDisplay2.setText(":");
    valueDisplay2.setVerticalAlignment(SwingConstants.CENTER);
    
    valueDisplay3 = new JLabel();
    valueDisplay3.setFont(new java.awt.Font("Lucida Grande", 0, 72));
    valueDisplay3.setPreferredSize(new Dimension(116, 73));
    valueDisplay3.setVerifyInputWhenFocusTarget(true);
    valueDisplay3.setHorizontalAlignment(SwingConstants.LEFT);
    valueDisplay3.setText("abc");
    valueDisplay3.setVerticalAlignment(SwingConstants.CENTER);
    
    
    
    modeDisplay = new JLabel();
    modeDisplay.setFont(new java.awt.Font("Lucida Grande", 0, 20));
    modeDisplay.setPreferredSize(new Dimension(130, 100));
    modeDisplay.setVerifyInputWhenFocusTarget(true);
    modeDisplay.setHorizontalAlignment(SwingConstants.CENTER);
    modeDisplay.setText("abc");
    modeDisplay.setVerticalAlignment(SwingConstants.CENTER );

    controlPanel.setLayout(gridLayout1);
    modePanel.setLayout(gridLayout2);
    valuePanel.setLayout(gridLayout1);
	valuePanel.add(valueDisplay1);
	valuePanel.add(valueDisplay2);
	valuePanel.add(valueDisplay3);
	valuePanel.add(modeDisplay); 
	valuePanel.setBackground( new Color(221,221,153));
    this.add(valuePanel, BorderLayout.CENTER);
    
    hourButton = new JButton("Hour");
    hourButton.setBackground(new Color(153,221,102));
    hourButton.setActionCommand(HOUR);
    hourButton.addActionListener(translation);
    // for depressed events
    hourButton.addMouseListener ( translation);

    minuteButton = new JButton("Minute");
    minuteButton.setBackground(new Color(153,221,102));
    minuteButton.setActionCommand(MINUTE);
    minuteButton.addActionListener(translation);
    // for depressed events
    minuteButton.addMouseListener( translation);
    
    controlPanel.add(hourButton);
    controlPanel.add(minuteButton);
    this.add(controlPanel,  BorderLayout.SOUTH);
    
    onButton = new JRadioButton("ON");
    onButton.setBackground(new Color(153,153,51));
    onButton.setActionCommand(ON);
    onButton.addActionListener(translation);
    
    offButton = new JRadioButton("OFF");
    offButton.setBackground(new Color(153,153,51));
    offButton.setActionCommand(OFF);
    offButton.addActionListener(translation);
    
    setButton = new JRadioButton("SET");
    setButton.setBackground(new Color(153,153,51));
    setButton.setActionCommand(SET);
    setButton.addActionListener(translation);
    
    ButtonGroup group = new ButtonGroup();
    group.add(onButton);
    group.add(offButton);
    group.add(setButton);
    group.setSelected( offButton.getModel() ,true);
    modePanel.add(onButton);
    modePanel.add(offButton);
    modePanel.add(setButton);
    this.add(modePanel,  BorderLayout.NORTH);
  } // end AlarmClockPresentation constructor.

  // ValueDisplay 1&3 display the time
  public void setValueDisplay(String hour, String minute) {
    System.out.println(hour + minute);
  	valueDisplay1.setText(format.format(Integer.parseInt(hour)));
    valueDisplay3.setText(format.format(Integer.parseInt(minute)));
  } // End setValueDisplay1.
  
 // Mode Display displays the current mode of clock
  public void setModeDisplay(String newValue) {
    modeDisplay.setText(newValue);
  } // End setModeDisplay.
  
  
   public void setOnState() {
   setModeDisplay("ON" );
   modeDisplay.setVisible ( true );
  
  } // End setOnState.

  public void setOffState() {
  	setModeDisplay("OFF" );
   modeDisplay.setVisible ( false );
  
  } // End setOffState.

  public void setSetState() {
   setModeDisplay("SET" );
   modeDisplay.setVisible ( true );
  
  } // End setSetState.
  
  public void setAlarmSoundState() {
  	setModeDisplay("ALARMING");
  }
  
  public void setAlarmDisplayState() {
  	modeDisplay.setVisible( true );
  	setModeDisplay( "Alarm Time");	
  }
  
  public void propertyChange(PropertyChangeEvent evt) {
    if ("time".equals(evt.getPropertyName())) {
    String time = (String) evt.getNewValue() ;
    int index = time.indexOf( ",");
    setValueDisplay(time.substring(0,index) , time.substring(index+1,time.length()));
    
    }
  }

} // end class AlarmClockPresentation.
